<template>
  <div class="app-container" :class="{ 'qiankun-microapp': isQiankun }">
    <!-- 主应用布局 -->
    <div class="main-layout">
      <router-view v-slot="{ Component }">
        <transition name="page-transition" mode="out-in">
          <keep-alive>
            <component :is="Component" />
          </keep-alive>
        </transition>
      </router-view>
    </div>

    <!-- 全局加载动画 -->
    <div class="global-loader" v-if="isLoading">
      <div class="loader-content">
        <div class="dna-helix">
          <div class="dna-strand" v-for="i in 20" :key="i"></div>
        </div>
        <div class="loader-text">Loading...</div>
      </div>
    </div>

    <!-- 浮动导航指示器 -->
    <div class="floating-nav-indicator" v-if="!isQiankun">
      <div class="nav-dots">
        <div 
          class="nav-dot" 
          v-for="(section, index) in sections" 
          :key="index"
          :class="{ active: currentSection === index }"
          @click="scrollToSection(index)"
        ></div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { computed, ref, onMounted, onUnmounted } from 'vue'
import { qiankunWindow } from 'vite-plugin-qiankun/dist/helper'

// 响应式数据
const isLoading = ref(false)
const currentSection = ref(0)

// 计算属性
const isQiankun = computed(() => qiankunWindow.__POWERED_BY_QIANKUN__)

// 页面章节
const sections = ref([
  { name: 'home', label: '首页' },
  { name: 'about', label: '关于我们' },
  { name: 'services', label: '服务' },
  { name: 'research', label: '研究' },
  { name: 'contact', label: '联系' }
])

// 滚动到指定章节
const scrollToSection = (index: number) => {
  const element = document.querySelector(`[data-section="${sections.value[index].name}"]`)
  if (element) {
    element.scrollIntoView({ 
      behavior: 'smooth',
      block: 'start'
    })
  }
}

// 监听滚动事件
const handleScroll = () => {
  const scrollPosition = window.scrollY
  const windowHeight = window.innerHeight
  
  // 计算当前章节
  const sectionIndex = Math.floor(scrollPosition / windowHeight)
  if (sectionIndex !== currentSection.value && sectionIndex < sections.value.length) {
    currentSection.value = sectionIndex
  }
}

// 生命周期
onMounted(() => {
  if (!isQiankun.value) {
    window.addEventListener('scroll', handleScroll, { passive: true })
  }
})

onUnmounted(() => {
  if (!isQiankun.value) {
    window.removeEventListener('scroll', handleScroll)
  }
})
</script>

<style>
/* 全局CSS变量 */
:root {
  /* 主色调 - 生物医药感 */
  --primary-blue: #1e3a8a;
  --primary-cyan: #0891b2;
  --accent-teal: #0f766e;
  --accent-emerald: #047857;
  
  /* 渐变色彩 */
  --gradient-primary: linear-gradient(135deg, #1e3a8a 0%, #0891b2 100%);
  --gradient-secondary: linear-gradient(135deg, #0f766e 0%, #047857 100%);
  --gradient-accent: linear-gradient(135deg, #0891b2 0%, #0f766e 100%);
  
  /* 中性色 */
  --white: #ffffff;
  --gray-50: #f9fafb;
  --gray-100: #f3f4f6;
  --gray-200: #e5e7eb;
  --gray-300: #d1d5db;
  --gray-400: #9ca3af;
  --gray-500: #6b7280;
  --gray-600: #4b5563;
  --gray-700: #374151;
  --gray-800: #1f2937;
  --gray-900: #111827;
  
  /* 阴影 */
  --shadow-sm: 0 1px 2px 0 rgba(0, 0, 0, 0.05);
  --shadow-md: 0 4px 6px -1px rgba(0, 0, 0, 0.1), 0 2px 4px -1px rgba(0, 0, 0, 0.06);
  --shadow-lg: 0 10px 15px -3px rgba(0, 0, 0, 0.1), 0 4px 6px -2px rgba(0, 0, 0, 0.05);
  --shadow-xl: 0 20px 25px -5px rgba(0, 0, 0, 0.1), 0 10px 10px -5px rgba(0, 0, 0, 0.04);
  
  /* 动画 */
  --transition-fast: 0.15s cubic-bezier(0.4, 0, 0.2, 1);
  --transition-normal: 0.3s cubic-bezier(0.4, 0, 0.2, 1);
  --transition-slow: 0.5s cubic-bezier(0.4, 0, 0.2, 1);
  
  /* 圆角 */
  --radius-sm: 0.375rem;
  --radius-md: 0.5rem;
  --radius-lg: 0.75rem;
  --radius-xl: 1rem;
  --radius-2xl: 1.5rem;
}

/* 全局样式重置 */
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

html {
  scroll-behavior: smooth;
  font-size: 16px;
  line-height: 1.6;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  color: var(--gray-900);
  background: var(--white);
  overflow-x: hidden;
}

/* 主应用容器 */
.app-container {
  min-height: 100vh;
  position: relative;
  background: var(--white);
}

/* 微应用环境下的样式 */
.qiankun-microapp {
  padding: 0;
  background-color: transparent;
}

.main-layout {
  padding: 0;
  background-color: transparent;
}

/* 页面过渡动画 */
.page-transition-enter-active,
.page-transition-leave-active {
  transition: all var(--transition-slow);
}

.page-transition-enter-from {
  opacity: 0;
  transform: translateY(30px) scale(0.95);
}

.page-transition-leave-to {
  opacity: 0;
  transform: translateY(-30px) scale(0.95);
}

/* 全局加载动画 */
.global-loader {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: var(--gradient-primary);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 9999;
  backdrop-filter: blur(10px);
}

.loader-content {
  text-align: center;
  color: var(--white);
}

.dna-helix {
  position: relative;
  width: 60px;
  height: 60px;
  margin: 0 auto 20px;
}

.dna-strand {
  position: absolute;
  width: 4px;
  height: 20px;
  background: var(--white);
  border-radius: 2px;
  animation: dnaRotate 2s infinite linear;
}

.dna-strand:nth-child(odd) {
  left: 50%;
  transform-origin: 50% 30px;
}

.dna-strand:nth-child(even) {
  right: 50%;
  transform-origin: 50% 30px;
}

@keyframes dnaRotate {
  0% {
    transform: rotate(0deg);
  }
  100% {
    transform: rotate(360deg);
  }
}

.loader-text {
  font-size: 1.1rem;
  font-weight: 500;
  letter-spacing: 0.1em;
  animation: pulse 2s infinite;
}

@keyframes pulse {
  0%, 100% {
    opacity: 1;
  }
  50% {
    opacity: 0.5;
  }
}

/* 浮动导航指示器 */
.floating-nav-indicator {
  position: fixed;
  right: 30px;
  top: 50%;
  transform: translateY(-50%);
  z-index: 1000;
  pointer-events: none;
}

.nav-dots {
  display: flex;
  flex-direction: column;
  gap: 12px;
  pointer-events: auto;
}

.nav-dot {
  width: 12px;
  height: 12px;
  border-radius: 50%;
  background: var(--gray-300);
  cursor: pointer;
  transition: all var(--transition-normal);
  position: relative;
}

.nav-dot:hover {
  background: var(--primary-cyan);
  transform: scale(1.2);
}

.nav-dot.active {
  background: var(--primary-blue);
  transform: scale(1.3);
  box-shadow: 0 0 0 4px rgba(30, 58, 138, 0.2);
}

.nav-dot::before {
  content: '';
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  width: 20px;
  height: 20px;
  border-radius: 50%;
  background: transparent;
  border: 2px solid transparent;
  transition: all var(--transition-normal);
}

.nav-dot.active::before {
  border-color: var(--primary-blue);
  animation: ripple 2s infinite;
}

@keyframes ripple {
  0% {
    transform: translate(-50%, -50%) scale(1);
    opacity: 1;
  }
  100% {
    transform: translate(-50%, -50%) scale(2);
    opacity: 0;
  }
}

/* 响应式设计 */
@media (max-width: 768px) {
  .floating-nav-indicator {
    right: 15px;
  }
  
  .nav-dot {
    width: 10px;
    height: 10px;
  }
  
  .nav-dot::before {
    width: 16px;
    height: 16px;
  }
}

/* 滚动条样式 */
::-webkit-scrollbar {
  width: 8px;
}

::-webkit-scrollbar-track {
  background: var(--gray-100);
}

::-webkit-scrollbar-thumb {
  background: var(--gray-300);
  border-radius: 4px;
}

::-webkit-scrollbar-thumb:hover {
  background: var(--gray-400);
}

/* 选择文本样式 */
::selection {
  background: var(--primary-cyan);
  color: var(--white);
}

::-moz-selection {
  background: var(--primary-cyan);
  color: var(--white);
}
</style> 